﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shrestha.Trigraph
{
    public class Filter
    {
        private const string dateFormatter = "yyyy-MM-dd";

        //public static WorkingSet GetFiltered(double minLat, double maxLat, double minLng, double maxLng, WorkingSet maximal)
        //{
        //    WorkingSet subSet = new WorkingSet();
        //    List<TriEvent> temp = new List<TriEvent>();

        //    DateTime dateMax = DateTime.MinValue, dateMin = DateTime.MaxValue;
        //    double latMin = double.MaxValue, lngMin = double.MaxValue, latMax = double.MinValue, lngMax = double.MinValue;

        //    foreach (TriEvent tg in maximal.TriEventList)
        //    {
        //        if ((tg.Latitude >= minLat && tg.Latitude <= maxLat) && (tg.Longitude >= minLng && tg.Longitude <= maxLng))
        //        {
        //            dateMax = dateMax < tg.EventDate ? tg.EventDate : dateMax;
        //            dateMin = tg.EventDate < dateMin ? tg.EventDate : dateMin;

        //            latMax = latMax < tg.Latitude ? tg.Latitude : latMax;
        //            latMin = tg.Latitude < latMin ? tg.Latitude : latMin;

        //            lngMax = lngMax < tg.Longitude ? tg.Longitude : lngMax;
        //            lngMin = tg.Longitude < lngMin ? tg.Longitude : lngMin;

        //            temp.Add(new TriEvent(tg.Latitude, tg.Longitude, tg.EventDate));
        //        }
        //    }

        //    subSet.MaxDbDate = dateMax;
        //    subSet.MinDbDate = dateMin;

        //    subSet.MinDbLat = latMin;
        //    subSet.MaxDbLat = latMax;
        //    subSet.MinDbLng = lngMin;
        //    subSet.MaxDbLng = lngMax;

        //    subSet.TriEventList = temp;
        //    subSet.TotalRows = subSet.TriEventList.Count;
        //    subSet.ShowLocationLines = true;

        //    return subSet;
        //}

        //public static WorkingSet GetFiltered(DateTime startDate, DateTime endDate, WorkingSet maximal)
        //{
        //    WorkingSet subSet = new WorkingSet();
        //    List<TriEvent> temp = new List<TriEvent>();

        //    DateTime dateMax = DateTime.MinValue, dateMin = DateTime.MaxValue;
        //    double latMin = double.MaxValue, lngMin = double.MaxValue, latMax = double.MinValue, lngMax = double.MinValue;

        //    foreach (TriEvent tg in maximal.TriEventList)
        //    {
        //        if (tg.EventDate >= startDate && tg.EventDate <= endDate)
        //        {
        //            dateMax = dateMax < tg.EventDate ? tg.EventDate : dateMax;
        //            dateMin = tg.EventDate < dateMin ? tg.EventDate : dateMin;

        //            latMax = latMax < tg.Latitude ? tg.Latitude : latMax;
        //            latMin = tg.Latitude < latMin ? tg.Latitude : latMin;

        //            lngMax = lngMax < tg.Longitude ? tg.Longitude : lngMax;
        //            lngMin = tg.Longitude < lngMin ? tg.Longitude : lngMin;

        //            temp.Add(new TriEvent(tg.Latitude, tg.Longitude, tg.EventDate));
        //        }
        //    }


        //    subSet.MaxDbDate = dateMax;
        //    subSet.MinDbDate = dateMin;

        //    subSet.MinDbLat = latMin;
        //    subSet.MaxDbLat = latMax;
        //    subSet.MinDbLng = lngMin;
        //    subSet.MaxDbLng = lngMax;

        //    subSet.TriEventList = temp;
        //    subSet.TotalRows = subSet.TriEventList.Count;
        //    subSet.ShowLocationLines = true;

        //    return subSet;
        //}

        //public static WorkingSet GetFiltered(double minLat, double maxLat, double minLng, double maxLng, DateTime startDate, DateTime endDate)
        //{
        //    string query = TriGraphResources.latName + "<" + maxLat.ToString() + " and " + TriGraphResources.latName + ">" + minLat.ToString() + " and " + TriGraphResources.lngName + "<" + maxLng.ToString() + " and " + TriGraphResources.lngName + ">" + minLng.ToString() + " and " + TriGraphResources.timeName + " between '" + startDate.ToString(dateFormatter) + "' and '" + endDate.ToString(dateFormatter) + "'";
        //    return ExecuteFilterQuery(query);
        //}

        public static WorkingSet GetFiltered(DateTime startDate, DateTime endDate, bool isClustered)
        {
            string query = null;
            string cquery = null;
            cquery = "min" + TriGraphResources.clusterDateName + " >'" + startDate.ToString(dateFormatter) + "' and max" + TriGraphResources.clusterDateName + "<'" + endDate.ToString(dateFormatter) + "'";
            query = TriGraphResources.timeName + " between '" + startDate.ToString(dateFormatter) + "' and '" + endDate.ToString(dateFormatter) + "'";
            return ExecuteFilterQuery(query, cquery, isClustered);
        }

        private static WorkingSet ExecuteFilterQuery(string query, string cquery, bool isClustered)
        {
            DbManager dbMan = new MySqlDbManager();
            WorkingSet subSet;
            subSet = dbMan.ExtractParameters(query, cquery, isClustered);
            return subSet;
        }

        public static WorkingSet GetFiltered(double minLat, double maxLat, double minLng, double maxLng, bool isClustered)
        {
            string query = TriGraphResources.latName + "<" + maxLat.ToString() + " and " + TriGraphResources.latName + ">" + minLat.ToString() + " and " + TriGraphResources.lngName + "<" + maxLng.ToString() + " and " + TriGraphResources.lngName + ">" + minLng.ToString();
            string cquery = "max" + TriGraphResources.clusterLatName + "<" + maxLat.ToString() + " and min" + TriGraphResources.clusterLatName + ">" + minLat.ToString() + " and max" + TriGraphResources.clusterLngName + "<" + maxLng.ToString() + " and min" + TriGraphResources.clusterLngName + ">" + minLng.ToString();
            return ExecuteFilterQuery(query, cquery, isClustered);
        }

        //public static WorkingSet GetFiltered(string query, bool isClustered)
        //{
        //    return ExecuteFilterQuery(query, isClustered);
        //}

        public static WorkingSet GetFiltered(double minLat, double maxLat, double minLng, double maxLng, DateTime startDate, DateTime endDate, bool isClustered)
        {
            return GetFiltered(minLat, maxLat, minLng, maxLng, startDate, endDate, isClustered);
        }


        //private static WorkingSet ExecuteFilterQuery(string filterQuery, bool isClustered)
        //{
        //    DbManager dbMan = new MySqlDbManager();
        //    History.Instance.Write(filterQuery);
        //    WorkingSet subSet;
        //    if (isClustered)
        //        subSet = dbMan.ExtractClusterParameters(filterQuery);
        //    else
        //        subSet = dbMan.ExtractParameters(filterQuery);
        //    return subSet;
        //}
    }
}
